package com.ptszyxx.popose.common.utils;


import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.widget.ImageView;

import com.ptszyxx.popose.R;
import com.ysg.utils.YImageUtil;
import com.ysg.utils.YStringUtil;

import java.io.IOException;
import java.lang.ref.WeakReference;

/**
 * 语音播放
 */
public class YVoiceUtil {
    private static YVoiceUtil instance;

    private YVoiceUtil(){}

    public static YVoiceUtil getInstance(){
        if (instance == null){
            synchronized (YVoiceUtil.class){
                if (instance == null){
                    instance = new YVoiceUtil();
                }
            }
        }
        return instance;
    }

    private MediaPlayer mediaPlayer;
    private int playStatus;//0.未播放，1.播放中，2.播放完成
    private WeakReference<ImageView> ivPreVoice;
    public void startPlay(ImageView ivVoice, String mp3UrL) {
        if (YStringUtil.isEmpty(mp3UrL)){
            return;
        }
        if (YStringUtil.eq(1, playStatus) && ivPreVoice != null){
            stopPlay();
            if (ivVoice == ivPreVoice.get()){
                return;
            }
        }
        ivPreVoice = new WeakReference<>(ivVoice);

        try {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(mp3UrL);//指定音频文件的路径
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (mediaPlayer != null) {
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    if (mediaPlayer != null) {
                        stopPlay();
                    }
                }
            });
            mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    if (mediaPlayer != null) {
                        stopPlay();
                    }
                    return true;
                }
            });
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                public void onPrepared(final MediaPlayer mp) {
                    playStatus = 1;
                    mp.start();//开始播放
                    YImageUtil.showGif(ivVoice, R.mipmap.gif_voice_play);
                }
            });
        }
    }

    public void stopPlay() {
        try {
            if (null != ivPreVoice && ivPreVoice.get() != null ){
                ImageView image = ivPreVoice.get();
                Activity context = (Activity)image.getContext();
                if (context != null && !context.isDestroyed()){
                    YImageUtil.show(ivPreVoice.get(), R.mipmap.ic_home_voice_start);
                }
            }
        }catch (Exception e){
        }

        if (null != mediaPlayer){
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
            playStatus = 2;
        }
    }



}
